using System;
using System.Collections.Generic;
using System.Text;
using Entity;
using ECommerce.Library.DataStructure;



namespace AutoGear.BLL
{
    public class CMS
    {
        #region CMS Private Fields
        int _cmsId;
        string _cmsContent = string.Empty;
        CMSType _cmsType;
        bool _isActive;
        bool _isDeleted;
        #endregion

        #region CMD Public Properties
        public int CMSId
        {
            get { return _cmsId; }
        }
        public string CMSContent
        {
            get { return _cmsContent; }
            set { _cmsContent = value; }
        }
        public CMSType CMSType
        {
            get { return _cmsType; }
            set { _cmsType = value; }
        }
        public bool IsActive
        {
            get { return this._isActive; }
        }
        public bool IsDeleted
        {
            get { return this._isDeleted; }
        }
        #endregion


        public CMS(int CmsId, string CmsType, string CmsContent, bool IsActive, bool IsDeleted)
        {
            this._cmsId = CmsId;
            try
            {
                this._cmsType = (CMSType)Enum.Parse(typeof(CMSType), CmsType, true);
            }
            catch (Exception ex)
            { 

            }
            this._cmsContent = CmsContent;
            this._isActive = IsActive;
            this._isDeleted = IsDeleted;
        }

        public CMS()
        {

        }
        public void Update()
        {
            try
            {
                TblCMS tblCms = TblCMS.GetTblCMSByCmsId(this._cmsId);
                tblCms.CmsContent = this._cmsContent;
                tblCms.Save();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public void Create()
        {
            try
            {
                this._cmsId = TblCMS.CreateNewTblCMS(this._cmsType.ToString(), this._cmsType.ToString(), this._cmsContent, true,false);
            }
            catch (Exception e)
            {
                throw e;
            }

        }
        
        public static CMS GetCMS(CMSType cmsType)
        {
            try
            {
                CMS cms = new CMS();
                ECommerce.DAL.CustomCollection<TblCMS> tblCMS = TblCMSExtended.GetCMSByCmsType(cmsType.ToString());
                ReadWriteCustomCollection<CMS> tmpCms = LoadFromCustomCollection(tblCMS);
                if (tmpCms.Count > 0)
                {
                    cms = tmpCms[0];
                }
                return cms;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static CMS GetCMS(int cmsID)
        {
            try
            {
                CMS cms = new CMS();
                ECommerce.DAL.CustomCollection<TblCMS> tblCMS = TblCMSExtended.GetCMSByCmsID(cmsID);
                ReadWriteCustomCollection<CMS> tmpCms = LoadFromCustomCollection(tblCMS);
                if (tmpCms.Count > 0)
                {
                    cms = tmpCms[0];
                }
                return cms;
            }
            catch (Exception e)
            {
                throw e;
            }
        }


        private static ReadWriteCustomCollection<CMS> LoadFromCustomCollection(ECommerce.DAL.CustomCollection<TblCMS> tblCms)
        {
            ReadWriteCustomCollection<CMS> result = new ReadWriteCustomCollection<CMS>();

            for (int i = 0; i < tblCms.Count; i++)
            {
                result.Add(new CMS(tblCms[i].CmsId, tblCms[i].CmsType, tblCms[i].CmsContent, tblCms[i].IsActive, tblCms[i].IsDeleted));
            }

            return result;
        }

    }
}
